const app = getApp()
const util = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor

Page(Monitor.hookApp({
  data: {
    sexesCustomization: false,
    arr: ['1', '2', '4', '6', '7'],
    authorization: true,
    mgqImg: `background:url( ${getApp().globalData.imgUrl}jsynewshop会员卷.png) ;background-size:100% 100%`,
    interval: 50,
    setNavigationBarTitle: '',
    tabBar: {},
    shares: {},
    searchVal: '',
    bannerData: {},
    fontData: {},
    imagesData: {},
    maps: {},
    formsData: {},
    iconData: {},
    btnData: {},
    index2: '',
    value: 0,
    userInfo: [],
    latitude: 0,
    longitude: 0,
    focus: false,
    autoplay: true,
    indicatorDots: false,
    activeIndex: [],
    activeIndex2: [],
    text: '',
    isload: true,
    postData: '',
    wztabbar: '',
    wztabBarName: '',
    clicknums: 1,
    animationsLock: false,
    arrsBar: '',
    isrun: true,
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    normalBtn: 'index'
  },
  wifidemo() {
    wx.navigateTo({
      url: '/pages/wifidemo/wifidemo',
    })
  },
  handleTouchStart(e) {
    this.setData({
      isrun: false
    })
  },
  handleTouchEnd() {
    this.setData({
      isrun: true
    })
  },
  preventTouchMove: function(event) {
    return false
  },
  closeNavimg() {
    this.setData({
      yindao: false
    })
  },
  shareBang(parentid) {
    console.log(parentid,'shareBang,parent_id')
    util.API('spwx/v1.Shop/bindFans', {
      parentid
    }).then(res => {})
  },
  showSomeTen(options) {
  
    // type 1 跳到分享和图
    // type 2 跳到商品详情
    // type 3 跳到正常页面
    // 'member_id=1&type=2&goods_id=1' //'mid=aWtr&type=2&gid=425'//
    var scene =  decodeURIComponent(options.scene);
    console.log(scene,'scene')
    var that = this;
    var type;
    var parent_id;
    var goods_id;
    if (scene == 'normal') {
      type = options.type;
      parent_id = options.member_id;
      goods_id = options.goods_id;
    } else if (scene != 'undefined') {
      var share = scene.split("&")
      type = share[1].split("=")[1]
      parent_id = share[0].split("=")[1];
      if (share[2]) {
        goods_id = share[2].split("=")[1]
      } else {
        goods_id = ''
      }
    }
    if (scene != 'undefined') {
      if (type == 1) {
        wx.navigateTo({
          url: '/pages/WoXuanDeHuo/WoXuanDeHuo?Ta=' + parent_id,
        })
      } else if (type == 2) {
        wx.navigateTo({
          url: `/pages/shopDetails/shopDetails?parent_id=${parent_id}&goods_id=${goods_id}`,
        })
      }
      this.shareBang(parent_id)
    }
  
  
  },
  formidSubmit(e){

    getApp().sendFromId(e.detail.formId)
  },
  onLoad: function(options) {
    this.showSomeTen(options)
    this.setData({
      yindao: !wx.getStorageSync('attention')
    });
    wx.setStorageSync('attention', true)
   
  },
  onShow(){
    var that = this
    wx.setNavigationBarTitle({
      title: getApp().globalData.app_name
    })
    if (!getApp().globalData.indexArr) {
      app.getARR().then(res => {
        that.jsonObjectFuns(res.styles)
        getApp().getPagesurl()
        getApp().authorization().then(res => {
          this.setData({
            authorization: res
          })
        })
      })
    } else {
      this.jsonObjectFuns(getApp().globalData.indexArr.styles)
      getApp().getPagesurl()
    }
    that.xingShou()
  },
  xingShou() {
    if (getApp().globalData.app_id == 'ZQ%3D%3D' || getApp().globalData.app_id == 'aQ%3D%3D' || getApp().globalData.app_id == 'Z2k%3D') {
      this.setData({
        sexesCustomization: true
      })
    }
  },
  jsonObjectFuns(jsonObject) {
    var that = this
    let jsonObjectEle = jsonObject[0].index.elements
    if (jsonObjectEle.filter(item => item.type == '2')[0]) {
      var le = jsonObjectEle.filter(item => item.type == '2')[0].content.length
    }
    this.setData({
      index: jsonObject[0].index,
      le: le ? le : '',
    })
  },
  onShareAppMessage(e) {
    var that = this
    return {
      title: getApp().globalData.app_name,
      imageUrl: e.from == 'button' ? getApp().globalData.shareImage : '',
      path: 'pages/index/index?scene=normal' + `&member_id=${getApp().globalData.member_id}&type=3`,
      success: () => {
   
      },
      fail: () => {
      
      }
    }
  },
  image_link(event) {
    var eInfo = event.currentTarget.dataset.item
    if (eInfo.type == '超值拼团') {
      eInfo.type = 1
    } else if (eInfo.type == '限时折扣') {
      eInfo.type = 2
    } else if (eInfo.type == '分销') {
      eInfo.type = 3
    }
    wx.navigateTo({
      url: eInfo.jump_url +
        '?goodsid=' +
        eInfo.jump_id +
        '&template_id=' +
        eInfo.jump_id +
        '&goodstypeid=' +
        eInfo.jump_id +
        '&type=' +
        (eInfo.type || '') +
        '&title=' +
        eInfo.title +
        '&jump_id=' +
        eInfo.jump_id +
        '&title=' +
        eInfo.jump_name +
        '&qtTitle=' +
        (eInfo.jump_title || '') +
        '&goods_id=' +
        eInfo.jump_id
    })
  },
  shopList(e) {

    var that = this
    util.API('spwx/v1.Shop/goodsinfo', {
      goods_id: e.currentTarget.dataset.goods
    }).then(res => {
      var {
        data,
        code
      } = res
      if (code === 200) {
        wx.navigateTo({
          url: '/pages/shopDetails/shopDetails?goods_id=' + e.currentTarget.dataset.goods
        })
      } else {
        wx.showToast({
          title: '该商品已经下架',
          icon: 'none'
        })
      }
    })



  },
  phoneCall(e) {
    var phoneNumber = e.currentTarget.dataset.phonenumber
    wx.makePhoneCall({
      phoneNumber: phoneNumber
    })
  },
  dizhi(e) {
    wx.openLocation({
      latitude: e.currentTarget.dataset.are.x,
      longitude: e.currentTarget.dataset.are.y,
      scale: 18,
      name: e.currentTarget.dataset.are.address
    })
  },
  onPullDownRefresh() {
    setTimeout(function() {
      wx.stopPullDownRefresh()
    }, 1000)
  },
  calling(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone, //此号码并非真实电话号码，仅用于测试
      success: function() {},
      fail: function() {}
    })
  },
  jumpSuosuo() {
    wx.navigateTo({
      url: '/pages/Search/Search'
    })
  },
  getCoupon(o) {
    var that = this
    let e = o.currentTarget.dataset
    let index = 'index.elements[' + e.idx + '].coupon[' + e.index + '].lingqu';

    util.API('spwx/v1.shop/get_coupon', {
      coupon_id: e.lg_coupon_id
    }).then(res => {
      let {
        code,
        msg
      } = res
      if (code == 200) {
        that.setData({
          [index]: true
        })
        wx.showToast({
          title: '领取成功',
        })
      } else {
        wx.showToast({
          title: msg,

        })
      }


    })
  },
  onHide: function () {
    clearInterval(this.data.interval);
  },
  onUnload: function () {
    clearInterval(this.data.intervals);
  },

}))